<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="850"
    draggable
    :align-center="true"
    @close="closeDialog"
    destroy-on-close
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="8"
          ><el-form-item label="设备编号" prop="deviceTypeCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deviceName"
              placeholder="请输入设备编号"
              clearable
            />

            <span v-else>{{ formData.deviceName }}</span>
          </el-form-item></el-col
        >
        <el-col :span="8"
          ><el-form-item label="设备名称" prop="deviceCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.deviceCode"
              placeholder="请输入设备编号"
              clearable
            />

            <span v-else>{{ formData.deviceCode }}</span>
          </el-form-item></el-col
        >
        <el-col :span="8"
          ><el-form-item label="采集时间" prop="collectionTime">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.collectionTime"
              placeholder="请输入采集时间"
              clearable
            />

            <span v-else>{{ formData.collectionTime }}</span>
          </el-form-item></el-col
        >
      </el-row>
    </el-form>
    <el-table
      v-loading="loading"
      :data="formData.dataList"
      highlight-current-row
      border
      max-height="325"
    >
      <el-table-column
        label="参数名"
        prop="parameterName"
        min-width="100"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        label="参数描述"
        prop="parameterDesc"
        min-width="100"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        label="参数值"
        prop="parameterValue"
        min-width="100"
        show-overflow-tooltip
        align="center"
      />
      <el-table-column
        label="单位"
        prop="measurementUnit"
        min-width="100"
        show-overflow-tooltip
        align="center"
      />
    </el-table>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "IOTDEVICEDATAADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  collectionTime: [],
  deviceTypeCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  deviceCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  data: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);
const tableData = ref([]);
let formData = reactive({
  id: "",
  collectionTime: "",
  deviceTypeCode: "",
  deviceCode: "",
  dataList: [],
  deviceName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    console.log(newVal);

    const { collectionTime, deviceName, deviceCode, dataList } = newVal;
    Object.assign(formData, {
      collectionTime,
      deviceCode,
      deviceName,
      dataList,
    });
    console.log(formData);
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.collectionTime = "";
  formData.deviceTypeCode = "";
  formData.deviceCode = "";
  formData.dataList = [];
}

const loading = ref(false);

/** 设备采集数据保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/iotadmin/iotdevicedata/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
